package org.ohos.rajawali3d.examples.examples.ui;

import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Slider;
import ohos.agp.render.opengl.EGLConfig;
import ohos.agp.render.opengl.GLES1X;
import ohos.app.Context;
import opengl.platform.ohos.Log;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.textures.ATexture;
import org.rajawali3d.materials.textures.Texture;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.primitives.Sphere;

/**
 * This example shows the addition of a {link TextureView} with properties
 * set in XML.
 */
public class AnimatedTextureViewFragment extends AExampleFragment {

    private Slider mSeekBarZ;
    private AnimatorProperty animatorProperty;

    @Override
    public AExampleRenderer createRenderer() {
        return new BasicRenderer(getContext(), this);
    }

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        animatorProperty = mRenderSurface.createAnimatorProperty();
        animatorProperty.rotate(360);
        animatorProperty.setDuration(20000);
        animatorProperty.setLoopedCount(AnimatorValue.INFINITE);
        animatorProperty.setCurveType(Animator.CurveType.BOUNCE);
        animatorProperty.setLoopedListener(new Animator.LoopedListener() {
            @Override
            public void onRepeat(Animator animator) {
                Log.d("Animation", "onRepeat:" + animator.toString() + " --> " + animator.getLoopedCount());
            }
        });
        animatorProperty.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                Log.d("Animation", "onRepeat:" + animator.toString() + " --> " + animator.getLoopedCount());
            }

            @Override
            public void onStop(Animator animator) {
                Log.d("Animation", "onRepeat:" + animator.toString() + " --> " + animator.getLoopedCount());
            }

            @Override
            public void onCancel(Animator animator) {
                Log.d("Animation", "onRepeat:" + animator.toString() + " --> " + animator.getLoopedCount());
            }

            @Override
            public void onEnd(Animator animator) {
                Log.d("Animation", "onRepeat:" + animator.toString() + " --> " + animator.getLoopedCount());
            }

            @Override
            public void onPause(Animator animator) {
                Log.d("Animation", "onRepeat:" + animator.toString() + " --> " + animator.getLoopedCount());
            }

            @Override
            public void onResume(Animator animator) {
                Log.d("Animation", "onRepeat:" + animator.toString() + " --> " + animator.getLoopedCount());
            }
        });
        return container;
    }

    private final class BasicRenderer extends AExampleRenderer {

        private Object3D mSphere;

        BasicRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            try {
                getCurrentScene().setBackgroundColor(0xFFADADAD);
                Material material = new Material();
                material.addTexture(new Texture("earthColors",
                        ResourceTable.Media_earthtruecolor_nasa_big));
                material.setColorInfluence(0);
                mSphere = new Sphere(1, 24, 24);
                mSphere.setMaterial(material);
                getCurrentScene().addChild(mSphere);
            } catch (ATexture.TextureException e) {
                e.printStackTrace();
            }

            getCurrentCamera().enableLookAt();
            getCurrentCamera().setLookAt(0, 0, 0);
            getCurrentCamera().setZ(6);
        }

        @Override
        public void onRender(final long elapsedTime, final double deltaTime) {
            super.onRender(elapsedTime, deltaTime);
            mSphere.rotate(Vector3.Axis.Y, 1.0);
        }

        @Override
        public void onRenderSurfaceCreated(EGLConfig config, GLES1X gl, int width, int height) {
            super.onRenderSurfaceCreated(config, gl, width, height);
            AnimatedTextureViewFragment.this.startAnimation();
        }
    }

    private void startAnimation() {
        animatorProperty.start();
    }
}
